﻿using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;

public class EditorHelper
{
    public static string ProcessCommand(string command, string argument)
    {
        System.Diagnostics.ProcessStartInfo start = new System.Diagnostics.ProcessStartInfo(command);
        start.Arguments = argument;
        start.CreateNoWindow = false;
        start.ErrorDialog = true;
        start.UseShellExecute = false;

        if (start.UseShellExecute)
        {
            start.RedirectStandardOutput = false;
            start.RedirectStandardError = false;
            start.RedirectStandardInput = false;
        }
        else
        {
            start.RedirectStandardOutput = true;
            start.RedirectStandardError = true;
            start.RedirectStandardInput = true;
            start.StandardOutputEncoding = System.Text.UTF8Encoding.UTF8;
            start.StandardErrorEncoding = System.Text.UTF8Encoding.UTF8;
        }

        System.Diagnostics.Process p = System.Diagnostics.Process.Start(start);
        p.WaitForExit();
        string error = p.StandardError.ReadToEnd();

        p.Close();
        return error;
    }

    public static void UpdateProgress(string info, int current, int total)
    {
        EditorUtility.DisplayProgressBar("Generate...", string.Format("{0} {1}/{2}", info, current, total), Mathf.InverseLerp(0, total, current - 1));
    }

    public static void CloseProgress()
    {
        EditorUtility.ClearProgressBar();
    }

    // 版本相关
    public enum VersionCodeFlag
    {
        MAJOR,
        MIDDLE,
        MINOR,
    }

    // 生成下一个版本号
    public static string NextVersion(string version, VersionCodeFlag flag = VersionCodeFlag.MINOR)
    {
        int[] code = GetVersionCode(version);
        for (int i = (int)flag + 1; i < code.Length; ++i)
        {
            code[i] = 1;
        }

        ++code[(int)flag];
        return GetVersion(code);
    }

    public static int[] GetVersionCode(string version)
    {
        string[] nums = version.Split(new[] { "." }, System.StringSplitOptions.RemoveEmptyEntries);
        int[] code = new int[nums.Length];
        for (int i = 0; i < nums.Length; ++i)
        {
            code[i] = int.Parse(nums[i]);
        }

        return code;
    }

    public static string GetVersion(int major, int middle, int minor)
    {
        return string.Format("{0}.{1:D2}.{2:D3}", major, middle, minor);
    }

    public static string GetVersion(int[] code)
    {
        return GetVersion(code[0], code[1], code[2]);
    }

    public static string FormatVersion(string version)
    {
        int[] code = GetVersionCode(version);
        return GetVersion(code);
    }

    public static void GetAllImages(Transform root, List<Image> images)
    {
        for (int i = 0; i < root.childCount; ++i)
        {
            Transform child = root.GetChild(i);
            Image image = child.GetComponent<Image>();
            if (image != null)
            {
                images.Add(image);
            }
            GetAllImages(child, images);
        }
    }

    #region 获取目录下所有xxx
    public static List<T> ExcuteDic<T>(string path, string type) where T : Object
    {
        List<T> list = new List<T>();
        if (!Directory.Exists(path))
        {
            return list;
        }
        string[] dirs = Directory.GetDirectories(path);
        foreach (string dir in dirs)
        {
            ExcutePath<T>(dir, type, ref list);
        }
        return list;
    }

    public static void ExcutePath<T>(string path, string type, ref List<T> list) where T : Object
    {
        if (Directory.Exists(path))
        {
            string[] dirs = Directory.GetDirectories(path);
            foreach (string t in dirs)
            {
                ExcutePath(t, type, ref list);
            }
        }
        string[] files = Directory.GetFiles(path);
        if (typeof(T) == typeof(Sprite) && path.EndsWith("emoji"))
        {
            return;
        }
        list.AddRange(from t in files where t.EndsWith("." + type) select AssetDatabase.LoadAssetAtPath<T>(t));
    }
    #endregion
}
